@svizzle/utils/[any-any]-[object-object]

Methods

(static) groupValuesWith(accessor) → {function}

Source:
Since:
  • 0.6.0
See:

Return a function expecting an object and returning an object grouping its values using the provided accessor. Note that this works by concatenating all values before grouping hence you can also directly index values being arrays (see the second example).

Example
> regroupedByX = groupValuesWith(obj => obj.x)

> // single values
> obj1 = {
  a: {x: 1, y: 2},
  b: {x: 3, y: 4},
  c: {x: 'a', y: 6},
  d: {x: 1, y: 8},
}
> regroupedByX(obj1)
{
  1: [{x: 1, y: 2}, {x: 1, y: 8}],
  3: [{x: 3, y: 4}],
  a: [{x: 'a', y: 6}],
}

> // values as arrays
> obj2 = {
  a: [{x: 1, y: 2}, {x: 3, y: 4}],
  b: [{x: 'a', y: 6}, {x: 1, y: 8}],
}
> regroupedByX(obj2)
{
  1: [{x: 1, y: 2}, {x: 1, y: 8}],
  3: [{x: 3, y: 4}],
  a: [{x: 'a', y: 6}],
}
Parameters:
Name Type Description
accessor function

Any -> Any

Returns:
  • Object -> Object
Type
function

(static) indexValuesWith(accessor) → {function}

Source:
Since:
  • 0.6.0
See:

Return a function expecting an object and returning an index of all its values using the provided accessor. Use this if you're sure that applying the accessor on the object values returns unique strings. Note that this works by concatenating all values before indexing hence you can also directly index values being arrays (see the second example).

Example
> reindexedByX = indexValuesWith(obj => obj.x)

> // single values
> obj1 = {
  unique1: {x: 'unique1', y: 2},
  unique2: {x: 'unique2', y: 4},
  unique3: {x: 'unique3', y: 6},
  unique4: {x: 'unique4', y: 8},
}
> reindexedByX(obj1)
{
  unique1: {x: 'unique1', y: 2},
  unique2: {x: 'unique2', y: 4},
  unique3: {x: 'unique3', y: 6},
  unique4: {x: 'unique4', y: 8},
}

> // values as arrays
> obj2 = {
  a: [{x: 'unique1', y: 2}, {x: 'unique2', y: 4}],
  b: [{x: 'unique3', y: 6}, {x: 'unique4', y: 8}],
}
> reindexedByX(obj2)
{
  unique1: {x: 'unique1', y: 2},
  unique2: {x: 'unique2', y: 4},
  unique3: {x: 'unique3', y: 6},
  unique4: {x: 'unique4', y: 8},
};
Parameters:
Name Type Description
accessor function

Any -> Any

Returns:
  • Object -> Object
Type
function

(static) mergeWith(fn) → {function}

Source:
Since:
  • 0.1.0

Return a function expecting two objects to merge using the provided merge function

Example
> mergeWithSubtract = mergeWith(_.subtract)
> mergeWithSubtract(
    {a: 8, b: 3},
    {a: 5, b: 2, c: 7}
)
{a: 3, b: 1, c: 7},
Parameters:
Name Type Description
fn function

Merge function

Returns:
  • Object -> Object
Type
function